
const path = require('path');

const { merge } = require('webpack-merge');
const TerserPlugin = require('terser-webpack-plugin');
const FileManagerPlugin = require('filemanager-plugin').WebpackFilemanager;

const common = require('./webpack.common.config');

const htmlWebpackPlugin = common.plugins.find(
  (p) => p.constructor.name === 'HtmlWebpackPlugin'
);
if (htmlWebpackPlugin) {
  htmlWebpackPlugin.userOptions.minify = {
    collapseWhitespace: true,
    conservativeCollapse: true,
    removeComments: true,
    removeRedundantAttributes: true,
    removeScriptTypeAttributes: true,
    removeStyleLinkTypeAttributes: true,
    useShortDoctype: true,
    minifyCSS: true,
    minifyJS: true,
  };
}

module.exports = merge(common, {
  mode: 'production',
  plugins: [
    // new HtmlWebpackPlugin({
    //   filename: path.resolve(__dirname, '../dist/main/index.html'),
    //   template: path.resolve(__dirname, `../public/index.html`),
    //   templateParameters: {
    //     entryPath: `${baseUrl}/entry.js?v=${+new Date()}`,
    //   },
    //   inject: false, // @todo 'body',
    //   // templateContent: {}, // function() {}
    // }),
    new FileManagerPlugin({
      events: {
        start: {
          del: {
            items: [path.resolve(__dirname, '../dist')],
          },
        },
        end: {
          move: {
            items: [
              // {
              //   source: path.resolve(__dirname, '../dist/index.html'),
              //   destination: path.resolve(__dirname, '../dist/main')
              // },
              {
                source: path.resolve(__dirname, '../dist/entry.js'),
                destination: path.resolve(__dirname, '../dist/static/'),
              },
              {
                source: path.resolve(__dirname, '../dist/pages'),
                destination: path.resolve(__dirname, '../dist/static'),
              },
            ],
          },
          copy: {
            items: [
              {
                source: path.resolve(__dirname, '../public/template'),
                destination: path.resolve(__dirname, '../dist/static'),
              },
            ],
          },
        },
      },
    }),
  ],
  optimization: {
    usedExports: false,
    minimize: true,
    minimizer: [
      new TerserPlugin({
        extractComments: false,
      }),
    ],
  },
});
